package com.sasu.university_data.controller.after;

import com.sasu.university_data.entity.CollegeInfo;
import com.sasu.university_data.service.CollegeInfoService;
import com.sasu.university_data.util.ResponseCode;
import com.sasu.university_data.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "学校图片+简介功能接口")
@RestController
@RequestMapping("/college")
public class CollegeInfoController {

    @Resource
    private CollegeInfoService collegeInfoService;

    @ApiOperation("分页查询数据")
    @GetMapping("/select")
    public ResponseData selectByPage( String name, Integer page, Integer pageSize){

        List<CollegeInfo> collegeInfos = collegeInfoService.selectByPage(name, page, pageSize);
        if (collegeInfos==null){
            return new ResponseData(ResponseCode.NULL_DATA);
        }

        long count = collegeInfoService.count(name);

        return new ResponseData(ResponseCode.SUCCESS,collegeInfos,count);
    }

    /**
     * 新增数据
     *
     * @param collegeInfo 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    public ResponseData add( @RequestBody CollegeInfo collegeInfo){

        if (!collegeInfoService.insert(collegeInfo)){

            return new ResponseData(ResponseCode.ADD_FAILED);
        }

        long count = collegeInfoService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 更新数据
     *
     * @param collegeInfo 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PostMapping("/update")
    public ResponseData edit(@RequestBody CollegeInfo collegeInfo){

        if (!collegeInfoService.update(collegeInfo)){
            return new ResponseData(ResponseCode.UPDATE_FAILED);
        }

        long count = collegeInfoService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @PostMapping("/delete")
    public ResponseData deleteById(Integer id){

        if (!collegeInfoService.deleteById(id)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = collegeInfoService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }


    /***
     * 通过id批量删除数据
     * @param ids
     * @return
     */
    @ApiOperation("通过id批量删除数据")
    @PostMapping("/lotDelete")
    public ResponseData deleteByIds(int[] ids){

        if (!collegeInfoService.deleteByIds(ids)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = collegeInfoService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }
}
